Skip to content

fix(maths): correct binomial_coefficient when n is 0 and r > 0 - #15685

Closed
saiyam-jain-dev wants to merge 1 commit into
TheAlgorithms:masterfrom
saiyam-jain-dev:fix/binomial-coefficient-zero-n
Closed

saiyam-jain-dev wants to merge 1 commit into
TheAlgorithms:masterfrom
saiyam-jain-dev:fix/binomial-coefficient-zero-n

Conversation

@saiyam-jain-dev

Copy link
Copy Markdown

Describe your change

  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.

Summary

In maths/binomial_coefficient.py, binomial_coefficient(n, r) used:

if 0 in (n, r):
    return 1

Because of this check, binomial_coefficient(0, r) returned 1 for any r > 0 (such as binomial_coefficient(0, 10) == 1).

Mathematically, the binomial coefficient $\binom{n}{r}$ represents the number of ways to choose $r$ elements from a set of $n$ elements. When $r > n \ge 0$, $\binom{n}{r} = 0$. For all other cases where $r > n$ (e.g. binomial_coefficient(5, 6) or binomial_coefficient(3, 5)), the function correctly returned 0. Only the $n = 0, r > 0$ case returned 1 incorrectly due to 0 in (n, r).

Changes

  1. Replace if 0 in (n, r): return 1 with standard boundary handling:
    if r == 0:
        return 1
    if r > n:
        return 0
    This ensures $\binom{0}{0} = 1$, $\binom{n}{0} = 1$, and $\binom{n}{r} = 0$ for all $r > n$ (including $n = 0$).
  2. Update doctests to reflect $\binom{0}{0} = 1$ and $\binom{0}{10} = 0$, matching math.comb.

Testing

  • Verified against math.comb(n, r) for all $n \in [0, 50]$ and $r \in [0, 50]$ (2,500 test cases with 0 mismatches).
  • pytest maths/binomial_coefficient.py --doctest-modules passed.
  • ruff check maths/binomial_coefficient.py passed.
  • ruff format --check maths/binomial_coefficient.py passed.

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Nov 1, 2026
@algorithms-keeper

Copy link
Copy Markdown

Multiple Pull Request Detected

@saiyam-jain-dev, we are extremely excited that you want to submit multiple algorithms in this repository but we have a limit on how many pull request a user can keep open at a time. This is to make sure all maintainers and users focus on a limited number of pull requests at a time to maintain the quality of the code.

This pull request is being closed as the user already has an open pull request. Please focus on your previous pull request before opening another one. Thank you for your cooperation.

User opened pull requests (including this one): #15685, #15645, #15644, #15643, #15642

@algorithms-keeper algorithms-keeper Bot closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant